package org.noote.LineDetection;

import org.noote.libs.camera.CameraInterface;
import org.noote.libs.camera.helper.Helper_Camera;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.Spinner;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class LineDetection_Activity extends Activity {
	protected static final int MSG_BEGIN = 0;
	protected static final int MSG_IND = 1;
	protected static final int MSG_FINISH = 2;

	private static final int SOURCE_GRAPHIC = 0;
	private static final int SOURCE_IMAGE = 1;
	private static final int SOURCE_CAMERA = 2;
	
	DetectView m_viewDetect;
	Graphic2DView m_viewGraphic;
	ImageView m_viewImage;
	CameraInterface m_camera = new CameraInterface();
	CameraPreview m_viewCameraPreview;
	
	int  m_iSource = SOURCE_GRAPHIC;

	protected ProgressDialog m_mProgressDialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		FrameLayout preview = (FrameLayout) findViewById(R.id.frameLayoutDessin);
		if(Helper_Camera.checkCameraHardware(getApplicationContext()))
		{
			if(!m_camera.open(Helper_Camera.getCamera()))
			{
				Toast.makeText(getApplicationContext(), "Cannot start camera !", Toast.LENGTH_LONG).show();
				return;
			}
			
			m_viewCameraPreview = new CameraPreview(this, m_camera);
			m_viewCameraPreview.setVisibility(View.GONE);
			preview.addView(m_viewCameraPreview);
		}

		{
			m_viewImage = new ImageView(this);
			m_viewImage.setVisibility(View.GONE);
			preview.addView(m_viewImage);
		}

		{
			m_viewGraphic = new Graphic2DView(this);
			m_viewGraphic.setVisibility(View.GONE);
			preview.addView(m_viewGraphic);
		}
		preview.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				
				pushBuffer();
			}
		});

		FrameLayout graph = (FrameLayout) findViewById(R.id.frameLayoutDetection);
		m_viewDetect = new DetectView(this);
		graph.addView(m_viewDetect);
		graph.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				m_viewDetect.showNext();
			}
		});
		
		Spinner spinnerView = (Spinner) findViewById(R.id.spinnerView);
		spinnerView.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				m_iSource = arg2;
				changeSource();
				m_viewDetect.clear();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				m_iSource = SOURCE_GRAPHIC;
				changeSource();
				m_viewDetect.clear();
			}
		});
		
		changeSource();
	}

	private void changeSource() {
		if(m_iSource == SOURCE_CAMERA)
		{
			m_viewCameraPreview.setVisibility(View.VISIBLE);
			m_viewImage.setVisibility(View.GONE);
			m_viewGraphic.setVisibility(View.GONE);
			m_viewCameraPreview.startPreview();
		}
		else if(m_iSource == SOURCE_IMAGE)
		{
			m_viewCameraPreview.stopPreview();
			m_viewCameraPreview.setVisibility(View.GONE);
			m_viewImage.setVisibility(View.VISIBLE);
			m_viewGraphic.setVisibility(View.GONE);
		}
		else if(m_iSource == SOURCE_GRAPHIC)
		{
			m_viewCameraPreview.stopPreview();
			m_viewCameraPreview.setVisibility(View.GONE);
			m_viewImage.setVisibility(View.GONE);
			m_viewGraphic.setVisibility(View.VISIBLE);
		}
	}

	void pushBuffer()
	{
		if(m_iSource == SOURCE_CAMERA)
		{
			m_viewCameraPreview.copy_into(m_viewDetect._detect);
		}
		else if(m_iSource == SOURCE_IMAGE)
		{
			m_viewImage.copy_into(m_viewDetect._detect);
		}
		else if(m_iSource == SOURCE_GRAPHIC)
		{
			m_viewGraphic.copy_into(m_viewDetect._detect);
		}

		mHandler.sendMessage(mHandler.obtainMessage(MSG_BEGIN, null));
		new Thread((new Runnable() {
	        @Override
	        public void run() {
	    		m_viewDetect.detect();

				mHandler.sendMessage(mHandler.obtainMessage(MSG_FINISH, null));
	        }
	    })).start();
	}
	
	final Handler mHandler = new Handler() {
	    @Override
		public void handleMessage(Message msg) {
	        switch (msg.what) {
	        case MSG_BEGIN:
	        	m_mProgressDialog = ProgressDialog.show(LineDetection_Activity.this, "", "", true);
	            break;
	        case MSG_IND:
	            if (m_mProgressDialog.isShowing()) {
	            	m_mProgressDialog.setMessage(((String) msg.obj));
	            }
	            break;
	        case MSG_FINISH:
	            if (m_mProgressDialog.isShowing()) {
	            	m_mProgressDialog.dismiss();
	            	
	            	m_viewDetect.show();
	            }
	            break;
	        }
	    }
	};
}